package com.pp.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pp.entity.*;
import com.pp.mapper.SuccessMapper;
import com.pp.service.SuccessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lanou on 18/2/1.
 */
@Service
public class SuccessServiceImpl implements SuccessService{
    @Resource
    private SuccessMapper successMapper;
    @Override
    public Map<String,Object> game(Game game,String userName) {
        Game game1 = successMapper.findGame(game);
        int uid = successMapper.findUser(userName);
        int gid = game1.getId();
        Money money = successMapper.findUserMoney(gid,uid);
        Map<String,Object> map = new HashMap<>();
        map.put("game",game1);
        map.put("uid",uid);
        map.put("money",money);
        return map;
    }

    @Override
    public void setMoney(int uid,int gid) {
        successMapper.setMoney(gid,uid);
    }

    @Override
    public Money queryMoney(int uid,int gid) {
        Money money = successMapper.queryMoney(gid,uid);
        return money;
    }

    @Override
    public List<Shop> shopping(int id, int gid) {
        List<Shop> list = successMapper.shopping(gid,id);
        return list;
    }

    @Override
    public PageInfo buycar(int pageNum,int uid) {
       // List<Shop> shops = successMapper.buycar(uid);
        PageHelper.startPage(pageNum,6);
        List<Shop> list = new ArrayList<>();
        list = successMapper.getBuy(uid);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }



    @Override
    public Money pay(int jine, int uid, int gid) {

        successMapper.pay(jine,uid,gid);
        Money money = successMapper.findPay(uid,gid);

        return money;
    }

    @Override
    public List<Buy> getShop(int did, int uid) {
        List<Buy> list = successMapper.getShop(did,uid);
        return list;
    }

    @Override
    public Map<String,Object> buy(int did, int uid, int gid) {
       Shop shop = successMapper.getMoney(did);
        int buyMoney = (int)shop.getManey();
        Money money = successMapper.getUserMoney(uid,gid);
        int userMoney = money.getMoney();

            Map<String,Object> map = new HashMap<>();
            map.put("buyMoney",buyMoney);
            map.put("userMoney",userMoney);
            return map;

    }

    @Override
    public void setBuy(int did, int uid, int gid,int allMoney) {
        successMapper.setBuy(did,uid);
        successMapper.setAllMoney(allMoney,uid,gid);
    }


}
